import { request } from '../request';

export interface DriverCardListSearchParams {
  personName: string;
  phone: string;
  state: string;
  entId: string;
  associationId: string;
  selEntId: string;
  includeSubordinateData: number;
}

export interface DriverCardListItem {
  personName?: string;
  phone?: string;
  entName?: string;
  state?: number;
  startTime?: string;
  endTime?: string;
  studyCardId?: string;
  isTemporary?: number;
}

/**
 * @description 添加线下培训
 */
export const createOfflinePlan = (data: any) =>
  request({
    method: 'post',
    url: '/safe/api/planTrain/add',
    data,
  });

/**
 * @description 编辑线下培训
 */
export const eidtOfflinePlan = (data: any) =>
  request({
    method: 'post',
    url: '/safe/api/planTrain/edit',
    data,
  });
/**
 * @description 删除线下培训
 */
export const delOfflineStudyPlan = (planCourseId: any) =>
  request({
    method: 'get',
    url: '/safe/api/planTrain/delete?planCourseId=' + planCourseId,
  });

/**
 * @description 线下培训修改状态
 */
export const updateOfflinePlanStatus = (params: any) =>
  request({
    method: 'get',
    url: '/safe/api/planTrain/updateOpenStatus',
    params,
  });
/***
 * @description 获取线下培训计划详情
 */
export const getOfflinePlanDetail = (planCourseId: string) =>
  request({
    method: 'get',
    url: '/safe/api/planTrain/details?planCourseId=' + planCourseId,
  });
/**
 * @description 线下导出URL
 */
export const getOffLineCodeUrl = (params: any) =>
  request({
    method: 'get',
    url: '/unicron/ent/Im/api/Friend/make/safePlanQrcode',
    params,
  });
/**
 * @desc 分页查询司机学习卡列表
 */
async function secDriverCardList(
  { personName, phone, state, entId, includeSubordinateData, associationId }: Partial<DriverCardListSearchParams>,
  pageIndex = 1,
  pageSize = 10,
) {
  const { data } = await request<{ list: DriverCardListItem[]; total: number }>({
    url: '/safe/api/studyCard/statisticsStudyCardListByWeb',
    data: {
      pageNum: pageIndex,
      pageSize: pageSize,
      personName,
      phone,
      state,
      entId: entId || undefined,
      associationId,
      includeSubordinateData,
    },
    noAutoShowErrorMessage: true,
  });

  return data;
}
/**
 * @desc 查询司机卡数量
 */
async function getCardNumValue(
  { personName, phone, state, entId, includeSubordinateData, associationId }: Partial<DriverCardListSearchParams>,
  pageIndex = 1,
  pageSize = 10,
) {
  const { data } = await request<unknown>({
    url: '/safe/api/studyCard/statisticsStudyCardNumByWeb',
    data: {
      pageNum: pageIndex,
      pageSize: pageSize,
      personName,
      phone,
      state,
      entId: entId || undefined,
      associationId,
      includeSubordinateData,
    },
    noAutoShowErrorMessage: true,
  });

  return data;
}
/**
 * @description 学习卡规则
 */
export const saveStudyCardConfig = (data: any) =>
  request({
    method: 'post',
    url: '/safe/api/studyCard/setting',
    data,
  });
/**
 * NOTE: 新接口
 *
 * @description 学习卡规则
 */
export const saveStudyCardConfigNew = (data: any) =>
  request({
    method: 'post',
    url: '/safe/api/studyCard/newSetting',
    data,
  });

/***
 * @description 获取学习卡规则
 */
export const getStudyCardConfig = (entId: string) =>
  request({
    method: 'get',
    url: `/safe/api/studyCard/ruleDetails?entId=${entId}`,
  });

/***
 * @description 获取学习卡详情
 */
export const getDriverCardDetail = (curCardId: string) =>
  request({
    method: 'get',
    url: '/safe/api/studyCard/statisticsStudyCardDetailsByWeb?studyCardId=' + curCardId,
    // data: {
    //   studyCardId: curCardId,
    // },
  });

export { getCardNumValue, secDriverCardList };
/***
 * @description 所属协会的企业跟司机数量
 */
export const getBelongEntlistApi = (entId: string) =>
  request({
    method: 'get',
    url: '/safe/api/studyCard/belongEntList?entId=' + entId,
  });
/***
 * @description 修改学习卡到期时间
 */
export const editExpireApi = (params: any) =>
  request({
    method: 'get',
    url: '/safe/api/studyCard/updateEndTime?studyCardId=' + params.studyCardId + '&endTime=' + params.upTime,
  });
/***
 * @description 获取线下培训人员情况
 */
type PersonDetail = {
  allNum: number;
  studyNum: number;
  unStudyNum: number;
  studyRate: number;
  signRule: string | number;
  personList?: any[];
};
export const getOfflinePlanPersonDetail = async (params: {
  date?: string;
  planCourseId: string;
  pageSize: number;
  pageIndex: number;
}) => {
  const { data } = await request<any>({
    method: 'get',
    url: '/safe/api/planTrain/trainPersonSituation',
    params: { ...params },
  });
  const c = data.list ? data.list : data;
  return {
    countData: {
      allNum: c.allNum || 0,
      studyNum: c.studyNum || 0,
      unStudyNum: c.unStudyNum || 0,
      studyRate: c.studyRate || 0,
    },
    signRule: c.signRule,
    list: c.personList || [],
    total: c.total,
  };
};
